package logic

import (
	"context"
	"exam/models/cathe"
	"exam/pkg"
	"fmt"
	"math/rand"
	"strconv"
	"time"

	"exam/examrpc/examrpc"
	"exam/examrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *examrpc.SendSmsRequest) (*examrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	intn := rand.Intn(9000) + 1000
	sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(intn))
	if err != nil {
		return nil, err
	}
	if *sms.Body.Code != "OK" {
		return nil, fmt.Errorf(*sms.Body.Message)
	}
	cathe.Rdb.Set(l.ctx, "sendSms"+in.Source+in.Mobile, intn, time.Minute*5)

	return &examrpc.SendSmsResponse{}, nil
}
